﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Sy;
using SYBOQry = Texfina.BOQry.Sy;

namespace SY
{
   public partial class wfUsuarioBnd : SearchPage
   {
      public const int _GRDLIST_STANULADO = 6;

      protected void Page_Load(object sender, EventArgs e)
      {

         this.LoadInitialSettings();
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }

         this.LoadData();
         SearchData();
      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Usuarios";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void RegisterGeneralScripts()
      {
         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var ctl = document.getElementById('" + txtidLogon.ClientID + "');");
         sb.AppendLine("ctl.value='';");
         sb.AppendLine("ctl = document.getElementById('" + txtdsNombres.ClientID + "');");
         sb.AppendLine("ctl.value='';");
         sb.AppendLine("return false;");

         bttClear.Attributes.Add("onclick", sb.ToString());

         sb = new StringBuilder();
         sb.AppendLine(bttSearch.ClientID + ".disabled=true;");
         sb.AppendLine(this.ClientScript.GetPostBackEventReference(bttSearch, "") + ";");

         bttSearch.Attributes.Add("onclick", sb.ToString());

      }

      public override void SearchData()
      {

         EUsuario objE = new EUsuario();
         if (this.txtidLogon.Text == "")
            objE.IdLogon = null;
         else
            objE.IdLogon = this.txtidLogon.Text;

         if (this.txtdsNombres.Text == "")
            objE.DsNombres = null;
         else
            objE.DsNombres = this.txtdsNombres.Text;

         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(SYBOQry.Usuario));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         String strQS = "";

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
               strQS = "wfUsuarioMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&idUser=";
               break;
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA = FormEditAction.AddNew;

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
            case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
         }

         string strQS = "wfUsuarioMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&idUser=" + dr["idUser"].ToString();
         Response.Redirect(strQS);

      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
         this.SearchData();
      }
   }
}